﻿namespace MDXStudio
{
    using MDXParser;
    using MDXStudio.QueryEditor;
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;
    using WeifenLuo.WinFormsUI.Docking;

    public class ParseWindow : DockContent
    {
        private IContainer components;
        private ImageList imageListParseTree;
        private TreeView treeParse;

        public ParseWindow()
        {
            this.InitializeComponent();
        }

        private ContextMenuStrip BuildParseTreeContextMenu(System.Windows.Forms.TreeNode treenode)
        {
            MDXNode tag = treenode.Tag as MDXNode;
            ContextMenuStrip strip = new ContextMenuStrip();
            ToolStripMenuItem item = new ToolStripMenuItem("Format", null, new EventHandler(this.OnParseTreeContextMenuClick), "mItemFormat");
            item.Tag = tag;
            strip.Items.Add(item);
            item = new ToolStripMenuItem("Show Dependencies", null, new EventHandler(this.OnParseTreeContextMenuClick), "mItemDependencies");
            item.Tag = tag;
            strip.Items.Add(item);
            item = new ToolStripMenuItem("Watch", null, new EventHandler(this.OnParseTreeContextMenuClick), "mItermDebugWatchValue");
            item.Tag = treenode;
            strip.Items.Add(item);
            if ((tag.GetExpNode() != null) && ((tag.GetExpNode().GetMDXType() == MDXDataType.Member) || (tag.GetExpNode().GetMDXType() == MDXDataType.Tuple)))
            {
                item = new ToolStripMenuItem("Watch Object", null, new EventHandler(this.OnParseTreeContextMenuClick), "mItermDebugWatchObject");
                item.Tag = treenode;
                strip.Items.Add(item);
                item = new ToolStripMenuItem("Coordinates", null, new EventHandler(this.OnParseTreeContextMenuClick), "mItermCoordinates");
                item.Tag = treenode;
                strip.Items.Add(item);
            }
            return strip;
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.components = new Container();
            ComponentResourceManager manager = new ComponentResourceManager(typeof(ParseWindow));
            this.treeParse = new TreeView();
            this.imageListParseTree = new ImageList(this.components);
            base.SuspendLayout();
            this.treeParse.Dock = DockStyle.Fill;
            this.treeParse.Font = new Font("Calibri", 9.75f, FontStyle.Regular, GraphicsUnit.Point, 0);
            this.treeParse.ImageIndex = 0;
            this.treeParse.ImageList = this.imageListParseTree;
            this.treeParse.Location = new Point(0, 0);
            this.treeParse.Name = "treeParse";
            this.treeParse.SelectedImageIndex = 0;
            this.treeParse.Size = new Size(0x124, 0x111);
            this.treeParse.TabIndex = 1;
            this.treeParse.AfterSelect += new TreeViewEventHandler(this.treeParse_AfterSelect);
            this.treeParse.MouseDown += new MouseEventHandler(this.treeParse_MouseDown);
            this.imageListParseTree.ImageStream = (ImageListStreamer) manager.GetObject("imageListParseTree.ImageStream");
            this.imageListParseTree.TransparentColor = Color.Transparent;
            this.imageListParseTree.Images.SetKeyName(0, "Cube.ico");
            this.imageListParseTree.Images.SetKeyName(1, "Dimension.ico");
            this.imageListParseTree.Images.SetKeyName(2, "Measure.ico");
            this.imageListParseTree.Images.SetKeyName(3, "MeasureCalculated.ico");
            this.imageListParseTree.Images.SetKeyName(4, "Kpi.ico");
            this.imageListParseTree.Images.SetKeyName(5, "FolderOpen.ico");
            this.imageListParseTree.Images.SetKeyName(6, "FolderClosed.ico");
            this.imageListParseTree.Images.SetKeyName(7, "Hierarchy.ico");
            this.imageListParseTree.Images.SetKeyName(8, "ParentChild.ico");
            this.imageListParseTree.Images.SetKeyName(9, "AttributeHierarchy.ico");
            this.imageListParseTree.Images.SetKeyName(10, "Level1.ico");
            this.imageListParseTree.Images.SetKeyName(11, "Level2.ico");
            this.imageListParseTree.Images.SetKeyName(12, "Level3.ico");
            this.imageListParseTree.Images.SetKeyName(13, "Level4.ico");
            this.imageListParseTree.Images.SetKeyName(14, "Level5.ico");
            this.imageListParseTree.Images.SetKeyName(15, "Level6.ico");
            this.imageListParseTree.Images.SetKeyName(0x10, "Level7.ico");
            this.imageListParseTree.Images.SetKeyName(0x11, "Level8.ico");
            this.imageListParseTree.Images.SetKeyName(0x12, "Level9.ico");
            this.imageListParseTree.Images.SetKeyName(0x13, "Level10.ico");
            this.imageListParseTree.Images.SetKeyName(20, "Level11.ico");
            this.imageListParseTree.Images.SetKeyName(0x15, "Level12.ico");
            this.imageListParseTree.Images.SetKeyName(0x16, "Level13.ico");
            this.imageListParseTree.Images.SetKeyName(0x17, "Level14.ico");
            this.imageListParseTree.Images.SetKeyName(0x18, "Level15.ico");
            this.imageListParseTree.Images.SetKeyName(0x19, "Level16.ico");
            this.imageListParseTree.Images.SetKeyName(0x1a, "NamedSet.ico");
            this.imageListParseTree.Images.SetKeyName(0x1b, "MDXQuery.ico");
            this.imageListParseTree.Images.SetKeyName(0x1c, "AttributeKey.ico");
            this.imageListParseTree.Images.SetKeyName(0x1d, "Member.ico");
            this.imageListParseTree.Images.SetKeyName(30, "ParentChildDisabled.ico");
            this.imageListParseTree.Images.SetKeyName(0x1f, "AttributeKeyDisabled.ico");
            this.imageListParseTree.Images.SetKeyName(0x20, "AttributeWithDisabledHierarchy.ico");
            this.imageListParseTree.Images.SetKeyName(0x21, "Script.ico");
            this.imageListParseTree.Images.SetKeyName(0x22, "CellCalculation.ico");
            this.imageListParseTree.Images.SetKeyName(0x23, "Scope.ico");
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.ClientSize = new Size(0x124, 0x111);
            base.Controls.Add(this.treeParse);
            base.HideOnClose = true;
            base.Icon = (Icon) manager.GetObject("$this.Icon");
            base.Name = "ParseWindow";
            base.TabText = "Parse Tree";
            this.Text = "Parse Tree";
            base.ResumeLayout(false);
        }

        private void OnParseTreeContextMenuClick(object sender, EventArgs e)
        {
            ToolStripMenuItem item = (ToolStripMenuItem) sender;
            string name = item.Name;
            if (name != null)
            {
                if (!(name == "mItemFormat"))
                {
                    if (!(name == "mItemDependencies"))
                    {
                        if (!(name == "mItermDebugWatchValue"))
                        {
                            if (!(name == "mItermDebugWatchObject"))
                            {
                                if (name == "mItermCoordinates")
                                {
                                    object obj1 = item.Tag;
                                }
                                return;
                            }
                            System.Windows.Forms.TreeNode node4 = item.Tag as System.Windows.Forms.TreeNode;
                            this.UpdateDebugWatch(node4, true);
                            return;
                        }
                        System.Windows.Forms.TreeNode treeNode = item.Tag as System.Windows.Forms.TreeNode;
                        this.UpdateDebugWatch(treeNode, false);
                        return;
                    }
                }
                else
                {
                    MDXNode node = item.Tag as MDXNode;
                    if (node != null)
                    {
                        TextBoxSource source = node.Source as TextBoxSource;
                        if (source == null)
                        {
                            return;
                        }
                        MdxEditor textBox = source.TextBox;
                        string formatMDX = node.GetFormatMDX();
                        textBox.AppendText(string.Format("\n{0}", formatMDX));
                        int num = (textBox.TextLength - formatMDX.Length) - 1;
                        if (num < 0)
                        {
                            num = 0;
                        }
                        textBox.SelectionStart = num;
                        textBox.SelectionLength = formatMDX.Length + 1;
                        textBox.Focus();
                        textBox.ScrollToCaret();
                    }
                    return;
                }
                MDXNode tag = item.Tag as MDXNode;
                if ((tag == null) || (tag.GraphVertex == null))
                {
                }
            }
        }

        private void treeParse_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                System.Windows.Forms.TreeNode selectedNode = this.treeParse.SelectedNode;
                if (selectedNode != null)
                {
                    MDXNode tag = selectedNode.Tag as MDXNode;
                    if (tag != null)
                    {
                        TextBoxSource source = tag.Source as TextBoxSource;
                        if (source != null)
                        {
                            MdxEditor textBox = source.TextBox;
                            int selectionStart = textBox.SelectionStart;
                            textBox.SelectAll();
                            textBox.SelectionBackColor = SystemColors.Window;
                            if (tag.Locator.Length != 0)
                            {
                                textBox.SelectionStart = tag.Locator.Position + source.StartLocation.Position;
                                textBox.SelectionLength = tag.Locator.Length;
                                textBox.SelectionBackColor = Color.Yellow;
                            }
                            else
                            {
                                textBox.SelectionStart = selectionStart;
                                textBox.SelectionLength = 0;
                            }
                            textBox.Focus();
                            textBox.ScrollToCaret();
                            textBox.SelectionLength = 0;
                            (textBox.Parent as DockContent).Show();
                            this.UpdateDebugWatch();
                        }
                    }
                }
            }
            catch (Exception)
            {
            }
        }

        private void treeParse_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                System.Windows.Forms.TreeNode nodeAt = this.treeParse.GetNodeAt(e.X, e.Y);
                if ((nodeAt != null) && (nodeAt.Tag is MDXNode))
                {
                    nodeAt.ContextMenuStrip = this.BuildParseTreeContextMenu(nodeAt);
                }
            }
        }

        private void UpdateDebugWatch()
        {
            (base.ParentForm as MainForm).UpdateDebugWatch();
        }

        private void UpdateDebugWatch(System.Windows.Forms.TreeNode treeNode, bool watchObject)
        {
            (base.ParentForm as MainForm).UpdateDebugWatch(treeNode, watchObject);
        }

        internal TreeView ParseTreeView
        {
            get
            {
                return this.treeParse;
            }
        }
    }
}

